package org.wu.framework.inner.lazy.orm.solon;


import org.noear.solon.core.AppContext;
import org.noear.solon.core.Plugin;
import org.wu.framework.core.exception.RuntimeExceptionFactory;
import org.wu.framework.inner.lazy.orm.solon.config.*;
import org.wu.framework.inner.lazy.orm.solon.processor.LazySchemaSolonAutoCreateProcessor;
import org.wu.framework.inner.lazy.orm.solon.prop.*;
import org.wu.framework.lazy.orm.core.config.prop.LazyDataSourceAttribute;
import org.wu.framework.lazy.orm.core.stereotype.LazyDS;
import org.wu.framework.lazy.orm.database.sql.expand.database.persistence.translation.LazyTableTranslation;

import javax.sql.DataSource;

/**
 * @author noear
 * @since 1.6
 */
public class XPluginImp implements Plugin {

    @Override
    public void start(AppContext context) {

        try {


        // 注入 获取应用
        context.beanMake(LazyApplicationSolonProperties.class);
        context.beanMake(LazyDataSourceSolonProperties.class);
        context.beanMake(LazyOperationSolonProperties.class);
        context.beanMake(LazySolonDdlProperties.class);
        // 导出配置
        context.beanMake(ExportDataSolonProperties.class);

        // 没有引入第三方数据源连接池 使用lazy 连接池
        context.beanMake(LazyDataSourceSolonAutoConfiguration.class);

        // 代理方法注入
        context.beanMake(LazyORMMethodSolonAutoConfiguration.class);
        // 注入 sql 审计
        context.beanMake(AuditSolonAutoConfiguration.class);

        // 重写代理数据源 、jdbc 适配器
        context.beanMake(LazyDataSourceProxySolonAutoConfiguration.class);
        // 适配器代理、
        context.beanMake(LazyOperationProxyBeanSolonAutoConfiguration.class);
        // 注入治愈
        context.beanMake(CureSolonAutoConfiguration.class);

        // 注入初始化执行
        // 注入自动化创建schema
            LazyDataSourceAttribute lazyDataSourceAttribute = context.getBean(LazyDataSourceSolonProperties.class);
            LazyOperationSolonProperties lazyOperationSolonProperties = context.getBean(LazyOperationSolonProperties.class);
            LazySchemaSolonAutoCreateProcessor lazySchemaSolonAutoCreateProcessor = new LazySchemaSolonAutoCreateProcessor(lazyDataSourceAttribute, lazyOperationSolonProperties);
            lazySchemaSolonAutoCreateProcessor.start();



        // smart 操作
        context.beanMake(LazySmartLazyOperationSolonAutoConfiguration.class);
        // lazy orm 自动转译工具
        context.beanMake(LazyTranslationSolonAutoConfigure.class);

        // 获取配置
        context.subWrapsOfType(DataSource.class, bw -> {
            //注入BeeFactory
//            context.wrapAndPut(SessionFactory.class, factory);
            System.out.println("监听到 DataSource ");
        });
        // 添加数据源代理对象 放到容器
        // 创建LazyLambdaStream 对象 而后放到容器
        //
        //for new
        context.beanBuilderAdd(LazyDS.class, (clz, wrap, anno) -> {
//            builderAddDo(clz, wrap, anno.value());
        });

        context.beanInjectorAdd(LazyDS.class, (varH, anno) -> {
//            injectorAddDo(varH, anno.value());
        });

        //for new
        context.beanBuilderAdd(LazyTableTranslation.class, (clz, wrap, anno) -> {
//            builderAddDo(clz, wrap, anno.value());
        });

        context.beanInjectorAdd(LazyTableTranslation.class, (varH, anno) -> {
//            injectorAddDo(varH, anno.value());
        });

        if (context.cfg().getBool("spring.lazy.enable-reverse-engineering", true)) {

            System.out.println("配置文件获取 spring.lazy.enable-reverse-engineering ：" + true);
        } else {
            System.out.println("配置文件获取 spring.lazy.enable-reverse-engineering ：" + false);
        }

        } catch (Throwable throwable) {
            RuntimeExceptionFactory.of(throwable);
        }
    }

}
